package com.yh.spring.struts.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yh.spring.mybatis.model.Page;
import com.yh.spring.mybatis.model.User;
import com.yh.spring.mybatis.service.IUserService;

/**
 * 用户控制器
 * @author Administrator
 *
 */

public class UserAction extends ActionSupport implements ModelDriven<Object>{

	private static final long serialVersionUID = 1L;
    /**
     * 注入用户有业务逻辑
     */
	@Autowired
	private IUserService userService;
	/**
	 * 用户对象参数
	 */
	private User user;
	/**
	 * 每页大小参数
	 */
	private int pageSize;
	/**
	 * 当前页码参数
	 */
	private int pageNum;
	/**
	 * 返回分页数据对象
	 */
	private Page page;
	
    
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	/**
	 * 根据用户名和用户密码查询该用户信息
	 */
	public String findUser() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		User u = new User();
		u = userService.findUser(name, password);
		request.getSession().setAttribute("data", u);
		return "success";	
	}
	/**
	 * 
	 * 模糊查询用户列表
	 */
	public String queryLikeUser(){
		Map<String,Object> cond = new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("user", user);
		page = userService.queryLikeUsers(cond);
		
		return "success";
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if(user==null){
			user = new User();
		}
		return user;
	}
	public String addUser(){
		 userService.addUser(user);
		 return "success";
	}
	public String delUser(){
		userService.delUser(user);
		return "success";
	}
	public String getUsers(){
		user = userService.getUser(user);
		ServletActionContext.getRequest().setAttribute("data", user);
		return "success";
	}
	public String editUsers(){
		userService.editUser(user);
		return "success";
	}
	

}
